#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

const int MAXN = 360;
const int MAXM = 150;
int N, M;
int cnt[5] = {0, 0, 0, 0, 0};
int gain[MAXN];
int f[50][50][50][50];

int main() {
    memset(f, 0, sizeof(f));
    cin >> N >> M;
    for (int i = 1; i <= N; i++) cin >> gain[i];
    for (int i = 1; i <= M; i++) {
        int card;
        cin >> card;
        cnt[card]++;
    }
    f[0][0][0][0] = gain[1];
    for (int i = 0; i <= cnt[1]; i++) {
        for (int j = 0; j <= cnt[2]; j++) {
            for (int k = 0; k <= cnt[3]; k++) {
                for (int l = 0; l <= cnt[4]; l++) {
                    if (!(i || j || k || l)) continue;
                    int pos = i + 2 * j + 3 * k + 4 * l + 1;
                    if (pos > 1 && i > 0)
                        f[i][j][k][l] =
                            max(f[i][j][k][l], f[i - 1][j][k][l] + gain[pos]);
                    if (pos > 2 && j > 0)
                        f[i][j][k][l] =
                            max(f[i][j][k][l], f[i][j - 1][k][l] + gain[pos]);
                    if (pos > 3 && k > 0)
                        f[i][j][k][l] =
                            max(f[i][j][k][l], f[i][j][k - 1][l] + gain[pos]);
                    if (pos > 4 && l > 0)
                        f[i][j][k][l] =
                            max(f[i][j][k][l], f[i][j][k][l - 1] + gain[pos]);
                }
            }
        }
    }

    cout << f[cnt[1]][cnt[2]][cnt[3]][cnt[4]];
}